// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    code: '',
    phoneLogin: false,
    codeCd: 0,
    url: '',
    params: {},
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      url: JSON.parse(options.url),
      params: JSON.parse(options.params)
    })
  },

  login(e) {
    let that = this
    let userInfo = this.data.userInfo
    wx.$https('wx.get.phone.number.login', {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      sessionKey: "",
      code: this.data.code,
      nickName: userInfo.nickName,
      avatarUrl: userInfo.avatarUrl,
      gender: userInfo.gender
    }).then(res => {
      that.loginCall(res)
    })
    // let mobile = this.data.mobile,
    //   verifyCode = this.data.code
    // if (mobile == '' || verifyCode == '') {
    //   wx.showToast({
    //     title: '请输入完整',
    //     icon: 'none'
    //   })
    //   return
    // }
    return
    wx.login({
      success(res) {
        console.log(res)
        wx.$https('account.login', {
          code: res.code,
          // mobile: mobile,
          // verifyCode: verifyCode,
        }).then(res => {
          that.loginCall(res)
        })
      }
    })
  },

  wxLogin() {
    let that = this
    wx.login({
      success(res) {
        wx.getUserInfo({
          success(res_) {
            that.setData({
              userInfo: res_.userInfo,
              code: res.code
            })
            that.changeWay()
            // wx.$https('account.login', {
            //   code: res.code,
            //   nickName: res_.userInfo.nickName,
            //   gender: res_.userInfo.gender,
            //   avatarUrl: res_.userInfo.avatarUrl,
            // }).then(res => {
            //   that.loginCall(res)
            // })
          }
        })
      }
    })
  },

  loginCall(res) {
    let that = this
    wx.setStorageSync('token', res.token)
    wx.showToast({
      title: '登录成功',
      mask: true
    })
    setTimeout(() => {
      if (that.data.url !== '') {
        wx.$redirectTo(`${that.data.url}`, that.data.params)
      } else {
        wx.switchTab({
          url: '/pages/home/home',
        })
      }
    }, 1000)
  },

  getCode() {
    let mobile = this.data.mobile
    if (this.data.codeCd != 0) return
    if (mobile == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    wx.$https('sms.code.get', {
      type: 1,
      mobile: mobile
    }).then(res => {
      wx.showToast({
        title: '验证码已发送',
        icon: 'none'
      })
      this.setData({
        codeCd: 60
      })
      this.codeSub()
    })
  },

  codeSub() {
    let that = this
    let codeCd = that.data.codeCd
    if (codeCd == 0) return
    setTimeout(() => {
      that.setData({
        codeCd: that.data.codeCd - 1
      })
      that.codeSub()
    }, 1000)
  },

  mobileInp(e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  codeInp(e) {
    this.setData({
      code: e.detail.value
    })
  },

  changeWay() {
    this.setData({
      phoneLogin: !this.data.phoneLogin
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})